import request from '@/utils/request'
import md5 from "md5";
// 账号密码登录
export const login = (params) => {
  return request.get('/auth', { params })
}

// 验证码登录
export const loginSmsCode = (params) => {
  return request.get('/auth/sms-code', { params })
}

//退出登录
export const loginOut = (params) => {
  return request.put('/auth/login-out', params)
}
export const randomWord = (randomFlag, min, max) => {
  var str = '',
      range = min,
      pos = '',
      arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min
  }
  for (var i = 0; i < range; i++) {
    pos = Math.round(Math.random() * (arr.length - 1))
    str += arr[pos]
  }
  return str
}
export const ToUrlParams = (obj) => {
  let string = ''
  for (let k in obj) {
    if (k !== 'sign' && obj[k] !== '' && !Array.isArray(obj[k])) {
      string += `${k}=${obj[k]}&`
    }
  }
  let result = string.replace(/(^&*)|(&*$)/g, '')
  return result
}
export const getSign = (data, { appid, appsecret, noncestr, timestamp, method = 'POST', key = 'yxp' }) => {
  let obj = {
    appid, appsecret, noncestr, method, timestamp, ...data
  }
  const ordered = {}
  Object.keys(obj).sort().forEach(function (key) {
    ordered[key] = obj[key]
  })
  let querystring = ToUrlParams(ordered)
  let result = `${querystring}${key}`
  let sign = md5(result)
  sign = sign.toUpperCase()
  return sign
}
//获取短信验证码
export const smsCode = (params) => {
  return request.post('/sms/verify-code', params)
}

//验证码修改密码
export const changePasswordBySms = (params) => {
  return request.put('/auth/password-by-sms-code', params)
}

//修改密码
export const changePassword = (params) => {
  return request.put('/auth/password', params)
}

//注册
export const register = (params) => {
  return request.post('/register', params)
}

// 获取用户信息
export const getUser = () => {
  return request.get('/auth/user')
}
